<script setup lang="ts">
import { ref } from 'vue';

defineProps<{
  isLoggedIn: boolean;
  currentUser: { username: string } | null;
}>();

const emit = defineEmits<{
  navigate: [view: string];
  logout: [];
}>();

const menuOpen = ref(false);

const toggleMenu = () => {
  menuOpen.value = !menuOpen.value;
};

const handleNavigate = (view: string) => {
  emit('navigate', view);
  menuOpen.value = false;
};

const handleLogout = () => {
  emit('logout');
  menuOpen.value = false;
};
</script>

<template>
  <header class="header">
    <div class="container">
      <div class="logo-container">
        <h1 class="logo" @click="handleNavigate('home')">
          CTF<span class="cursor"></span>
        </h1>
      </div>
      
      <!-- 右上角登出按钮 -->
      <button v-if="isLoggedIn" @click="handleLogout" class="logout-button-small">退出</button>
      
      <!-- 桌面导航 -->
      <nav class="nav-desktop">
        <ul>
          <li><button @click="handleNavigate('home')" class="nav-button">首页</button></li>
          <li><button @click="handleNavigate('challenges')" class="nav-button">挑战</button></li>
          <li><button @click="handleNavigate('leaderboard')" class="nav-button">排行榜</button></li>
          <li><button @click="handleNavigate('about')" class="nav-button">关于</button></li>
          <li v-if="!isLoggedIn">
            <button @click="handleNavigate('home')" class="nav-button login-button">登录</button>
          </li>
          <li v-else class="user-menu">
            <div class="user-info">
              <span class="username">{{ currentUser?.username }}</span>
            </div>
          </li>
        </ul>
      </nav>
      
      <!-- 移动端菜单按钮 -->
      <div class="menu-toggle" @click="toggleMenu">
        <div class="hamburger"></div>
      </div>
    </div>
    
    <!-- 移动端导航 -->
    <nav class="nav-mobile" :class="{ 'open': menuOpen }">
      <ul>
        <li><button @click="handleNavigate('home')" class="nav-button">首页</button></li>
        <li><button @click="handleNavigate('challenges')" class="nav-button">挑战</button></li>
        <li><button @click="handleNavigate('leaderboard')" class="nav-button">排行榜</button></li>
        <li><button @click="handleNavigate('about')" class="nav-button">关于</button></li>
        <li v-if="!isLoggedIn">
          <button @click="handleNavigate('home')" class="nav-button login-button">登录</button>
        </li>
        <li v-else>
          <div class="user-info-mobile">
            <span class="username">{{ currentUser?.username }}</span>
          </div>
        </li>
      </ul>
    </nav>
  </header>
</template>

<style scoped>
.header {
  background-color: rgba(255, 255, 255, 0.1);
  border-bottom: 3px solid rgba(255, 255, 255, 0.3);
  padding: 1em 0;
  position: sticky;
  top: 0;
  z-index: 1000;
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2em;
  position: relative;
}

.logo-container {
  flex-shrink: 0;
}

.logo {
  margin: 0;
  font-size: 1.8em;
  cursor: pointer;
  transition: all 0.3s ease;
  color: white;
  text-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}

.logo:hover {
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

/* 桌面导航 */
.nav-desktop ul {
  list-style: none;
  display: flex;
  margin: 0;
  padding: 0;
  align-items: center;
}

.nav-desktop li {
  margin-left: 1em;
}

.nav-button {
  background: none;
  border: none;
  color: white;
  font-size: 1em;
  padding: 0.5em 1em;
  cursor: pointer;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 1px;
  border-radius: 4px;
  font-weight: 500;
}

.nav-button:hover {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
}

.login-button {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  font-weight: bold;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.login-button:hover {
  background-color: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.4);
}

/* 用户菜单 */
.user-menu {
  position: relative;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 1em;
}

.username {
  color: white;
  font-weight: 500;
}

.logout-button {
  background-color: transparent;
  border: 1px solid rgba(238, 90, 36, 0.5);
  color: var(--color-error);
  padding: 0.5em 1em;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logout-button:hover {
  background-color: var(--color-error);
  color: white;
  box-shadow: 0 0 10px rgba(238, 90, 36, 0.3);
  border-color: var(--color-error);
}

/* 右上角小尺寸登出按钮 */
.logout-button-small {
  position: absolute;
  top: 10px;
  right: 20px;
  background-color: transparent;
  border: 1px solid rgba(238, 90, 36, 0.5);
  color: var(--color-error);
  padding: 0.3em 0.6em;
  font-size: 0.8em;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 1001;
}

.logout-button-small:hover {
  background-color: var(--color-error);
  color: white;
  box-shadow: 0 0 10px rgba(238, 90, 36, 0.3);
  border-color: var(--color-error);
}

/* 移动端菜单 */
.menu-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.5em;
}

.hamburger {
  width: 30px;
  height: 3px;
  background-color: var(--color-primary);
  position: relative;
  transition: all 0.3s ease;
}

.hamburger::before,
.hamburger::after {
  content: '';
  position: absolute;
  width: 30px;
  height: 3px;
  background-color: var(--color-primary);
  transition: all 0.3s ease;
}

.hamburger::before {
  top: -8px;
}

.hamburger::after {
  top: 8px;
}

.nav-mobile {
  background-color: rgba(255, 195, 18, 0.95);
  position: fixed;
  top: 60px;
  left: 0;
  right: 0;
  transform: translateY(-100%);
  transition: transform 0.3s ease;
  z-index: 999;
  border-bottom: 3px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.nav-mobile.open {
  transform: translateY(0);
}

.nav-mobile ul {
  list-style: none;
  margin: 0;
  padding: 1em;
  display: flex;
  flex-direction: column;
}

.nav-mobile li {
  margin: 0.5em 0;
}

.user-info-mobile {
  display: flex;
  flex-direction: column;
  gap: 0.5em;
  padding: 1em;
  border-top: 1px solid rgba(255, 195, 18, 0.2);
  margin-top: 0.5em;
}

/* 移动端右上角登出按钮调整 */
@media (max-width: 768px) {
  .logout-button-small {
    top: 8px;
    right: 60px;
    font-size: 0.7em;
    padding: 0.2em 0.5em;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-desktop {
    display: none;
  }
  
  .menu-toggle {
    display: block;
  }
}
</style>